package com.vikutorika.dao.imp;

import com.vikutorika.dao.GroupInfoDAO;
import com.vikutorika.dao.BasicDAO;
import com.vikutorika.entity.GroupInfo;
import com.vikutorika.entity.GroupTextMessage;
import com.vikutorika.util.V_DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GroupInfoDAOImp extends BasicDAO<GroupInfo> implements GroupInfoDAO {
    @Override
    public List<GroupInfo> getAll() {
        Connection conn = null;
        String sql = "select * from group_info;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn, sql, new BeanListHandler<>(GroupInfo.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public int totalRow() {
        Connection conn = null;
        String sql = "select count(1) from group_info;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            Long count = queryRunner.query(conn, sql, new ScalarHandler<>());
            return count.intValue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public List<GroupInfo> getByOwnerUID(Integer uid) {
        Connection conn = null;
        List<GroupInfo> list;
        String sql = "select * from group_info where group_owner_uid = ? ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            list = queryRunner.query(conn, sql, new BeanListHandler<>(GroupInfo.class), uid);
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public GroupInfo getByGID(Integer gid) {
        Connection conn = null;
        GroupInfo groupInfo;
        String sql = "select * from group_info where gid = ? ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            groupInfo = queryRunner.query(conn, sql, new BeanHandler<>(GroupInfo.class), gid);
            return groupInfo;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public GroupInfo getByGroupName(String group_name) {
        Connection conn = null;
        GroupInfo groupInfo;
        String sql = "select * from group_info where group_name = ? ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            groupInfo = queryRunner.query(conn, sql, new BeanHandler<>(GroupInfo.class), group_name);
            return groupInfo;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            V_DBUtils.close(conn);
        }
    }
}
